﻿using System;
using System.Windows.Forms;

using Daq331m;

namespace ADC_Demo
{
    public partial class Form1 : Form
    {
        private readonly Daq331M _daq331;
        private const uint VoltageRange = (uint)DaqVoltage.Voltage15V;
        private const uint SampleRate = (uint)DaqSampleRate.SampleRate100K;
        private const byte ChannelState = (byte)DaqAdcChannel.AinAll;
        private double[][] _receiveData;
        private double[] _xData;
        
        public Form1()
        {
            InitializeComponent();

            _daq331 = new Daq331M();

            formsPlot1.Plot.SetAxisLimitsY(-1, 16);
            formsPlot1.Plot.SetAxisLimitsX(0, 1000);
            
            // 启用图例显示
            formsPlot1.Plot.Legend(enable: true, location: ScottPlot.Alignment.UpperRight);
            
            formsPlot1.Refresh();
        }

        private void ConnectPushButtonClick(object sender, EventArgs e)
        {
            _daq331.DeleteDevice();
            _daq331.CreateDevice();

            _daq331.UseTcpClientBackend("192.168.1.100", 8000);

            // 初始化设备
            Console.WriteLine(!_daq331.InitializeDevice() ? "DAQ331M设备初始化失败" : "DAQ331M设备初始化成功");

            // 配置采集参数
            Console.WriteLine(!_daq331.ConfigureAdcParameters(SampleRate, VoltageRange)
                ? "配置DAQ331M采样率与电压失败"
                : "配置DAQ331M采样率与电压成功");
            
            // 配置采集通道
            Console.WriteLine(!_daq331.ConfigAdcChannel(ChannelState) ? "配置DAQ331M通道失败" : "配置DAQ331M通道成功");
            
            var enableChannelSize = _daq331.GetAdcEnableChannelSize();
            var storageDepth = (uint)_daq331.GetCurrentSampleRate() / enableChannelSize;
            var legalChannelSize = _daq331.GetAdcLegalChannelSize();

            _receiveData = new double[legalChannelSize][];
            for (var i = 0; i < legalChannelSize; i++)
            {
                _receiveData[i] = new double[storageDepth];
            }
            
            _xData = new double[storageDepth];
            for (var index = 0; index < storageDepth; ++index)
            {
                _xData[index] = 1000.0 / storageDepth * index;
            }
        }

        private void StartPushButtonClick(object sender, EventArgs e)
        {
            readDataTimer.Start();

            var startResult = _daq331.StartCollection();

            Console.WriteLine(!startResult ? @"DAQ331M开始采集数据失败" : @"DAQ331M开始采集数据");
        }

        private void StopPushButtonClick(object sender, EventArgs e)
        {
            var stopResult = _daq331.StopCollection();
            
            Console.WriteLine(!stopResult ? @"DAQ331M停止采集数据失败" : @"DAQ331M停止采集数据");

            readDataTimer.Stop();    
        }

        private void read_data_timer_Tick(object sender, EventArgs e)
        {
            var enableChannelSize = _daq331.GetAdcEnableChannelSize();
            var storageDepth = (uint)_daq331.GetCurrentSampleRate() / enableChannelSize;
            var legalChannelSize = _daq331.GetAdcLegalChannelSize();

            // 判断当前数据是否已经满足读取的条件
            if (!_daq331.AdcDataIsReady(storageDepth))
            {
                return;
            }

            var readResult = true;
            for (byte channel = 0; channel < legalChannelSize; channel++)
            {
                // 判断通道是否开启
                if ((ChannelState & (1 << channel)) == 0)
                {
                    continue;
                }

                // 读取数据
                readResult = _daq331.TryReadAdcData(channel, _receiveData[channel], storageDepth);
                if (!readResult)
                {
                    Console.WriteLine(@"Error");
                }
                
            }

            if (!readResult) return;
            {
                formsPlot1.Plot.Clear();

                for (var channel = 0; channel < legalChannelSize; channel++)
                {
                    // 判断通道是否开启
                    if ((ChannelState & (1 << channel)) == 0)
                    {
                        continue;
                    }
                    
                    // 为每个通道添加带标签的散点图，用于图例显示
                    formsPlot1.Plot.AddScatter(_xData, _receiveData[channel], label: $"通道 {channel + 1}");
                }

                formsPlot1.Refresh();
            }

        }
    }
}
